﻿using Mobile.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
using Mobile.Models;

namespace Mobile.Tests
{
    
    
    /// <summary>
    ///This is a test class for GuestUIControllerTest and is intended
    ///to contain all GuestUIControllerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GuestUIControllerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            string accessCode = "331533";
            string partyID = "dcac28fa-b322-404e-87fa-7f189b64d103";
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GuestUIController Constructor
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("C:\\Users\\Jim\\Documents\\Senior Year\\CS 638\\CurrentSolution\\TFS_Solution\\Mobile\\Mobile", "/")]
        [UrlToTest("http://localhost:1034/")]
        public void GuestUIControllerConstructorTest()
        {
            GuestUIController target = new GuestUIController();
            //Just want to ensure this value is not null
            Assert.IsNotNull(target);
            
            //Check that we start a service object
            //Assert.IsNotNull(target.sv);
        }

        /// <summary>
        ///A test for AddSong
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("C:\\Users\\Jim\\Documents\\Senior Year\\CS 638\\CurrentSolution\\TFS_Solution\\Mobile\\Mobile", "/")]
        [UrlToTest("http://localhost:1034/")]
        public void AddSongTest()
        {
            GuestUIController target = new GuestUIController(); // Initialize Controller
            string accessCode = "331533"; //Initialized to our default party
            System.Web.Mvc.ViewResult expected = new System.Web.Mvc.ViewResult(); // Initialize to type we are expecting
            System.Web.Mvc.ViewResult actual;

            string partyID = "55f55676-a894-4b65-8060-5c93c649efa2";
            
            actual = (System.Web.Mvc.ViewResult)target.AddSong();
            
            //Unsure how to exactly define whole System.Web.Mvc.ViewResult, but want to at least check that output is correct type
            Type x = actual.GetType();
            Type y = expected.GetType();
            Assert.AreEqual(y, x, "Output is of expected type", null);

            //Make sure the result we return is not null
            Assert.IsNotNull(actual);

            // Assert our data is correctly being returned from the database           
            Party party = (Party)actual.ViewData.Model;
            
            Assert.AreEqual(partyID, party.PartyID);
            
        }

        /// <summary>
        ///A test for GetPartyID
        ///</summary>
        [TestMethod()]
        [UrlToTest("http://localhost:1034/")]
        [DeploymentItem("Mobile.dll")]
        public void GetPartyIDTest()
        {
            GuestUIController_Accessor target = new GuestUIController_Accessor(); // initialize controller accessor
            string accessCode = "331533"; // set access code to our default value
            string expected = null; // Since we are running locally, don't expect to connect and thus code should return null
            string actual;
            actual = target.GetPartyID(accessCode);
            //Testing our error handling - actual should be null when running locally, as we can't connect to server and thus should
            //error out and return null
            Assert.AreEqual(expected, actual);

            //Once on server...
            try
            {
                expected = "55f55676-a894-4b65-8060-5c93c649efa2";

                actual = target.GetPartyID(accessCode);
                Assert.IsNotNull(actual, "Access Code does not map to a correct PartyID");

                Assert.AreEqual(expected, actual, "Party ID being returned does not match what we expect");
            }
            catch (Exception ex)
            {
                //if we catch an error, we must be running locally
            }

            //Now testing the regex functionality
            accessCode = "##3cb6356f-0a68-42b4-a60a-0c541cacca66#";
            expected = "3cb6356f-0a68-42b4-a60a-0c541cacca66";
            actual = target.GetPartyID(accessCode);
            
            Assert.AreEqual(expected, actual,"Regex returned different ID than expected");
        }

        /// <summary>
        ///A test for Index
        ///</summary>
        [TestMethod()]
        [UrlToTest("http://localhost:1034/")]
        public void IndexTest()
        {
            GuestUIController target = new GuestUIController(); // Initialize generic controller
            ActionResult expected = new RedirectToRouteResult(null); // Initialize 'expected' to type we are expecting
            ActionResult actual;
            FormCollection collection = new FormCollection();

            actual = target.Index(collection);
            //we want to make sure we are correctly returning of type 'View'
            Type expectedType = expected.GetType();
            Type actualType = actual.GetType();
            Assert.AreEqual(expectedType, actualType);
            
            //Let's also assert we don't return a null view
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for Playlist
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("C:\\Users\\Jim\\Documents\\Senior Year\\CS 638\\CurrentSolution\\TFS_Solution\\Mobile\\Mobile", "/")]
        [UrlToTest("http://localhost:1034/")]
        public void PlaylistTest()
        {
            GuestUIController target = new GuestUIController(); // Initialize controller

            String partyID = "55f55676-a894-4b65-8060-5c93c649efa2";
            //Set Expected value
            ActionResult expected = new System.Web.Mvc.ViewResult(); // Initializing to type we want to test
            ActionResult actual = new System.Web.Mvc.ViewResult();
            actual = target.Playlist();
            //Unsure how to exactly define whole System.Web.Mvc.ViewResult, but want to at least check that output is correct type
            Type x = actual.GetType();
            Type y = expected.GetType();
            Assert.AreEqual(y, x, "Output is of expected type",null);
            //Assert that the object returned is not null
            Assert.IsNotNull(actual);

            ViewResult output = (System.Web.Mvc.ViewResult)actual;

            // Assert our data is correctly being returned from the database           
            Party party = (Party)output.ViewData.Model;

            Assert.AreEqual(partyID, party.PartyID);
        }

        /// <summary>
        ///A test for UpvoteButton
        ///</summary>
        [TestMethod()]
        [UrlToTest("http://localhost:1034/")]
        [DeploymentItem("Mobile.dll")]
        public void UpvoteButtonTest()
        {
            GuestUIController_Accessor target = new GuestUIController_Accessor(); // initialize accessor
            string songID, PartyID;
            songID = "5e26f4c5-b15a-4685-b56e-2f72d9e9ceb2";
            PartyID = "55f55676-a894-4b65-8060-5c93c649efa2";
            ActionResult expected = new System.Web.Mvc.ViewResult(); // TODO: Initialize to an appropriate value
            ActionResult actual;
            actual = target.UpvoteButton(songID);
            //we will be testing the output's type as it is hard to replicate the view object
            Type expectedType = expected.GetType();
            Type actualType = actual.GetType();
            Assert.AreEqual(expectedType, actualType);

            //Make sure we are at least getting a value
            Assert.IsNotNull(actual);

            //cast output to view result so we can test that it has correct parameters
            System.Web.Mvc.ViewResult output = (System.Web.Mvc.ViewResult)actual;

            // Assert our data is correctly being returned from the database           
            Queue party = (Queue)output.ViewData.Model;

            Assert.AreEqual(PartyID, party.partyID);
            
            //test that correct song was modified
            foreach(Song x in party.SongList)
            {
                if (x.ID.Equals(songID))
                {
                    
                }
            }

        }
    }
}
